import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  UseGuards,
  Req,
  HttpStatus,
  HttpCode,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
import { TaskCommentService } from './task-comment.service';
import { 
  CreateTaskCommentDto, 
  UpdateTaskCommentDto,
  TaskCommentResponseDto 
} from './dto';
import { Permissions } from '../../decorators/permissions.decorator';
import { PermissionsGuard } from '../../guards/permissions.guard';
import { plainToClass } from 'class-transformer';

@ApiTags('任务评论')
@Controller('comments')
@UseGuards(JwtAuthGuard, PermissionsGuard)
@ApiBearerAuth()
export class TaskCommentController {
  constructor(private readonly taskCommentService: TaskCommentService) {}

  @Post()
  @Permissions('task:comment:create')
  @ApiOperation({ summary: '创建任务评论' })
  @ApiResponse({ 
    status: HttpStatus.CREATED, 
    description: '评论创建成功', 
    type: TaskCommentResponseDto 
  })
  async createComment(
    @Body() createTaskCommentDto: CreateTaskCommentDto,
    @Req() req,
  ) {
    const userId = req.user.id;
    const comment = await this.taskCommentService.createComment(
      createTaskCommentDto,
      userId,
    );
    return plainToClass(TaskCommentResponseDto, comment);
  }

  @Get('task/:taskId')
  @Permissions('task:comment:read')
  @ApiOperation({ summary: '获取任务的所有评论' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取成功', 
    type: [TaskCommentResponseDto] 
  })
  async getTaskComments(@Param('taskId') taskId: string) {
    const comments = await this.taskCommentService.getTaskComments(taskId);
    return comments.map(comment => plainToClass(TaskCommentResponseDto, comment));
  }

  @Get(':id')
  @Permissions('task:comment:read')
  @ApiOperation({ summary: '获取评论详情' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取成功', 
    type: TaskCommentResponseDto 
  })
  async getCommentById(@Param('id') id: string) {
    const comment = await this.taskCommentService.getCommentById(id);
    return plainToClass(TaskCommentResponseDto, comment);
  }

  @Put(':id')
  @Permissions('task:comment:update')
  @ApiOperation({ summary: '更新评论' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '更新成功', 
    type: TaskCommentResponseDto 
  })
  async updateComment(
    @Param('id') id: string,
    @Body() updateTaskCommentDto: UpdateTaskCommentDto,
    @Req() req,
  ) {
    const userId = req.user.id;
    const comment = await this.taskCommentService.updateComment(
      id,
      updateTaskCommentDto,
      userId,
    );
    return plainToClass(TaskCommentResponseDto, comment);
  }

  @Delete(':id')
  @Permissions('task:comment:delete')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: '删除评论' })
  @ApiResponse({ status: HttpStatus.NO_CONTENT, description: '删除成功' })
  async deleteComment(@Param('id') id: string, @Req() req) {
    const userId = req.user.id;
    await this.taskCommentService.deleteComment(id, userId);
  }
} 